package com.itheima.controller;


import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealByIdVo;
import com.itheima.vo.SetmealVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    //添加套餐
    @PostMapping
    public CommonResult addSetmeal(@RequestBody SetmealDto setmealDto){
        boolean res = setmealService.addSetmeal(setmealDto);
        if (!res){
            return CommonResult.error("添加套餐失败");
        }
        return CommonResult.success("添加套餐成功");
    }

    //套餐分页查询
    @GetMapping("page")   //name判断了为空的情况，其他为空情况没判断，因此设置一个默认值@RequestParam
    public CommonResult page(@RequestParam(required = false) String name ,
                             //默认page为1
                             @RequestParam(defaultValue = "1") Integer page ,
                             //默认pageSize为10
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<SetmealVo> page1 = setmealService.page(name,page, pageSize);
        return CommonResult.success(page1);
    }


    //根据id获取套餐
    @GetMapping("/{id}")
    public CommonResult getSetmealById(@PathVariable String id){
        SetmealByIdVo setmealByIdVo = setmealService.getSetmealById(id);
        return CommonResult.success(setmealByIdVo);
    }


    //修改套餐
    @PutMapping
    public CommonResult updateSetmeal(@RequestBody SetmealDto setmealDto){
        boolean res = setmealService.updateSetmeal(setmealDto);
        if (!res){
            return CommonResult.error("修改套餐失败");
        }
        return CommonResult.success("修改套餐成功");

        //牛逼写法
//        return new CommonResult(res ? 200 : 500, res ? "修改套餐失败":"修改套餐成功",null);
    }


    //套餐上架/下架
    @PostMapping("/status/{status}")
    public CommonResult status(@PathVariable Integer status,  @RequestParam List<String> ids){
        boolean res = setmealService.status(status,ids);
        if (!res){
            return CommonResult.error("套餐上架/下架失败");
        }
        return CommonResult.success("套餐上架/下架成功");
    }


    //删除套餐
    @DeleteMapping
    public CommonResult deleteSetmeal(@RequestParam List<String> ids){
        boolean res = setmealService.deleteSetmeal(ids);
        if (!res){
            return CommonResult.error("删除套餐失败");
        }
        return CommonResult.success("删除套餐成功");
    }

}
